package com.itheima.mall.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.pojo.OrderReturnReason;
import com.itheima.mall.service.OrderReturnReasonService;

import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/returnReason")
public class OrderReturnReasonController {

    @Autowired
    private OrderReturnReasonService orderReturnReasonService;


    /**
     * 分页查询退货原因
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<PageResult> FindList(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize){
        //创建分页信息
        Page<OrderReturnReason> returnReasonIPage = new Page<>(pageNum,pageSize);
        //创建分页构造器
        LambdaQueryWrapper<OrderReturnReason> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(OrderReturnReason::getId);
        //调用方法
        Page<OrderReturnReason> page = orderReturnReasonService.page(returnReasonIPage, qw);
        //构造返回参数
        PageResult init = PageResult.init(page);
        return ResponseEntity.ok(init);
    }



    /**
     * 添加退货原因
     * @param orderReturnReason
     * @return
     */
    @PostMapping("/create")
    private ResponseEntity create(@RequestBody OrderReturnReason orderReturnReason) {
        if (StringUtils.isEmpty(orderReturnReason.getName())) {
            return ResponseEntity.ok("退货原因不能为空");
        }
        //设置当前时间
        orderReturnReason.setCreateTime(LocalDateTime.now());
        orderReturnReasonService.save(orderReturnReason);
        return ResponseEntity.ok("退货原因添加成功");
    }


    /**
     * 批量删除退货原因
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestParam List<Integer> ids){
        orderReturnReasonService.removeByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 修改退货原因启用状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/update/status")
    public ResponseEntity updateStatus(@RequestParam List<Integer> ids,Integer status){
        //查询当前的实体类
        Integer integer = ids.get(0);
        OrderReturnReason orderReturnReason = orderReturnReasonService.getById(integer);
        orderReturnReason.setStatus(status);
        orderReturnReasonService.updateById(orderReturnReason);
        return ResponseEntity.ok("退货状态修改成功");
    }

    /**
     * 获取单个退货原因详情信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<OrderReturnReason> getOne(@PathVariable("id") Integer id){
        OrderReturnReason orderReturnReason = orderReturnReasonService.getById(id);
        return ResponseEntity.ok(orderReturnReason);
    }


    /**
     * 修改退货原因
     * @param id
     * @param orderReturnReason
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity update(Integer id,@RequestBody OrderReturnReason orderReturnReason){
        orderReturnReasonService.updateById(orderReturnReason);
        return ResponseEntity.ok("修改退货原因成功");
    }
}
